/**
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.active;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.apache.http.Header;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.login.NeedLoginFragment;
import com.tstudy.laoshibang.mode.Active;
import com.tstudy.laoshibang.mode.response.ActiveApplyResponse;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
@SuppressWarnings({ "deprecation" })
@EFragment(R.layout.apply)
public class ApplyFragment extends NeedLoginFragment{
	final static String TAG = "apply";
	public ApplyFragment() {
		super();
	}
	
	@ViewById(R.id.apply_root)
	RelativeLayout mRootLayout;
	
	@ViewById(R.id.apply_progress)
	RelativeLayout mProgressLayout;
	
	@ViewById(R.id.apply_active_people_value)
	EditText mUserNameExt;
	@ViewById(R.id.apply_active_mobile_value)
	EditText mMobileExt;
	@ViewById(R.id.apply_active_email_value)
	EditText mEmailExt;
	
	@ViewById(R.id.apply_active_pic)
	ImageView mActivePic;
	
	@ViewById(R.id.apply_active_title)
	TextView mActiveTitle;
	
	@ViewById(R.id.apply_active_address)
	TextView mActiveAddress;
	
	@ViewById(R.id.apply_active_time)
	TextView mApplyActiveTime;
	
	
	
	
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	
	public static final int BANNER_DURATION = 3000;
	int mCurrentPosition = 0;
	List<View> mViewList = new ArrayList<View>();
	List<Active> mActives = new ArrayList<Active>();
	@StringRes(R.string.select_all)
	String mSelectAllStr;
	int mSelectAllKey = -1;
	public Active mActive;

	public static void add(int containerViewId,Active active) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
//		args.putInt(CONSTANT.ARGS.ACTIVE_ID, actId);
		Fragment fragment = Fragment.instantiate(BaseApplication.mContext,ApplyFragment_.class.getName(),args);
		((ApplyFragment)fragment).mActive = active;
		((BaseActivity) BaseApplication.mCurrentActivity).add(containerViewId, fragment, TAG);
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@AfterViews
	public void initView(){
		if (mIsFirstLoad) {
			mRootLayout.setOnClickListener(null);
			if (mActive != null) {
				if (!TextUtils.isEmpty(mActive.imgIdxNames)) {
					HttpManager.getInstance().loadCommonImage(mActivePic, mActive.imgIdxNames.split(",")[0]);
				}
				if (!TextUtils.isEmpty(mActive.bmTime)) {
					mApplyActiveTime.setText(mActive.bmTime);
				}
				mActiveTitle.setText(mActive.title);
					if (!TextUtils.isEmpty(mActive.title)) {
				}
				if (!TextUtils.isEmpty(mActive.unit.address)) {
					mActiveAddress.setText(mActive.unit.address);
				}
			}
			
			if (!TextUtils.isEmpty(BaseApplication.mCurrentUser.getEmail())) {
				mEmailExt.setText(BaseApplication.mCurrentUser.getEmail());
			}
			if (!TextUtils.isEmpty(BaseApplication.mCurrentUser.getUserName())) {
				mUserNameExt.setText(BaseApplication.mCurrentUser.getUserName());
			}
			if (!TextUtils.isEmpty(BaseApplication.mCurrentUser.getMobile())) {
				mMobileExt.setText(BaseApplication.mCurrentUser.getMobile());
			}
		}
	}
	@Click({R.id.apply_back,R.id.apply_action})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.apply_back:
			backAction(mFragmentId);
			break;
		case R.id.apply_action:
			activeApply();
			break;
		default:
			break;
		}
		showSoftKeyBoard(mEmailExt, false);
		showSoftKeyBoard(mMobileExt, false);
		showSoftKeyBoard(mUserNameExt,false);
	}
	/**
	 * 活动报名
	 */
	private void activeApply(){
		if (mIsLoading) {
			return;
		}
		if (!CommonUtil.checkEmail(mEmailExt.getText().toString())) {
			BaseApplication.showToast(R.string.email_validate);
			return;
		}
		if (TextUtils.isEmpty(mUserNameExt.getText())) {
			BaseApplication.showToast(R.string.username_validate);
			return;
		}
		if (!CommonUtil.checkMobile(mMobileExt.getText().toString())) {
			BaseApplication.showToast(R.string.regist_error_mobile_illegal);
			return;
		}
		mIsLoading = true;
		BaseListJsonHandler<ActiveApplyResponse> response = new BaseListJsonHandler<ActiveApplyResponse>() {
			public void onStart() {
				showProgressBar(mProgressLayout);
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, ActiveApplyResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					ApplyResultFragment.add(mFragmentId,response.data.sucesMsg);
				}else{
					BaseApplication.showToast(response.getErrMsg());
				}
				afterLoading();
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, String rawJsonData,
					ActiveApplyResponse errorResponse) {
				afterLoading();
				super.onFailure(statusCode, headers, throwable, rawJsonData, errorResponse);
			}
			@Override
			protected ActiveApplyResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
//				afterLoading();
				return mGson.fromJson(rawJsonData,ActiveApplyResponse.class);
			}
		};
		HttpManager.getInstance().activeApply(BaseApplication.mUserNo, mActive.actId,mUserNameExt.getText().toString(), mMobileExt.getText().toString(), mEmailExt.getText().toString(), response);
		
	}

	public void afterLoading(){
		mIsLoading = false;
		hideProgressBar(mProgressLayout);
	}
	@Override
	public void clear() {
		showSoftKeyBoard(mEmailExt, false);
		showSoftKeyBoard(mMobileExt, false);
		showSoftKeyBoard(mUserNameExt, false);
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
